<?php
	session_start();
	include_once("conn.php");
	
	$tid = $_GET["tid"];
	$board_name = $_GET["board"];

	//下面这段查出版主名字，当前用户为版主，则显示出“删除”选项
	$sql = "select s_admin_name from bbs_session where s_name = '".$board_name."'";
	$result = mysql_query($sql);
	$q_result = mysql_fetch_array($result);
	$s_admin_name = $q_result["s_admin_name"];	//版主名


	if(!isset($_SESSION["hit"]) )
	{
		
		$sql = "update bbs_topic set t_click_count = t_click_count+1 where t_id = '".$tid."'";
		$result = mysql_query($sql);
		if(!$result)
		{
			echo "<script>alert('点击数没+1');</script>";
		}

		//unset($_SESSION["hit"]);	//注销hit
		$_SESSION["hit"] = 1;
	}

		//下面这段用于读出点击数
		$sql = "select t_click_count from bbs_topic where t_id = '".$tid."'";
		$result = mysql_query($sql);
		$q_result = mysql_fetch_array($result);
		$t_click_count = $q_result["t_click_count"];		//主帖点击数



	$sql = "select * from bbs_topic where t_id = '".$tid."'";
	$result = mysql_query($sql);
	$q_result = mysql_fetch_array($result);

	//接下来，取出浏览帖子需要的 标题、内容、作者、发表时间、其他如回复数，点击数稍后实现。。

	$post_board = $board_name;	//发表版面
	$post_name = $q_result["t_topic"];
	$post_content = $q_result["t_content"];
	$post_author = $q_result["t_author"];
	$post_time2 = $q_result["t_time"];		//原来是$post_time,全段，为了只截取时间改了
	$post_time = substr($post_time2,5,14);	//只截取月、日 时间
	$post_time_year = substr($post_time2,0,4);	//截取年份


	$post_week = $q_result["t_week"];		//英文星期，英文月份，天数，年份
	$post_time_week = substr($post_week,0,3);	//截取星期（英文）

	$sql2 = "select count(r_id) as reply_num_in_topic from bbs_reply where r_tid = '".$tid."'";
	$result_reply_num = mysql_query($sql2);
	$q_result_reply_num = mysql_fetch_array($result_reply_num);
	$reply_num_in_topic = $q_result_reply_num["reply_num_in_topic"];				//取得当前主题的回复数量

	//接下来代码为：分页显示回帖代码
	
	$page_size = 10;	//设置每页最大显示回帖量
	if($reply_num_in_topic%$page_size == 0)
	{
		$page_count = intval($reply_num_in_topic/$page_size);	//总页数
	}
	else {
		$page_count = ceil($reply_num_in_topic/$page_size);			//总页数
	}
	//判断页数
	if(isset($_GET["page"]))
	{
		$page_num = intval($_GET["page"]);
	}
	else {
		$page_num = 1;
	}




	//接下来是取读评论的代码

	$offset = ($page_num-1)*$page_size;				//此变量用于limit查询;
/*
	$sql = "select * from bbs_reply where r_tid = '".$tid."' order by r_time limit ".$offset.",".$page_size."";
	$result = mysql_query($sql);        //此条变量为“回复集”功能，在后面用到，此$result变量 不能被覆盖！！！！请用别的变量名！！
*/

	$level = 0;					//回帖楼层标签 ，一开始为0，其后自加。



	//取文件夹中的所有表情
function getsmiles() {
  if ($handle = opendir('images/smiles')) {
    while (false !== ($file = readdir($handle))) {
      if (preg_match('/(.+?)[.]gif/ie',$file, $name)) {
        $result[$name[1]]=$file;
      }
    }
    closedir($handle);
    return $result;
  }else{
    return;
  }
}

// 评论表情

function displaysmiley($smile) {
  $smile=htmlspecialchars($smile);
  if(is_file('images/smiles/'.$smile.'.gif')) {
    $img='<img src="./images/smiles/'.$smile.'.gif" >';
  }
  return $img;
}


?>
<html>
<head>
<meta charset="utf-8" />
<link rel = "stylesheet" href = "browse.css" type = "text/css" />
<link rel = "stylesheet" href = "sidebar.css" type = "text/css" />
<script src = "jquery-1.7.1.js"></script>
<script>
$(function(){
	$("#div_hidden").click(function(){
		$("#div_reply").slideToggle();
		$("#div_face").slideToggle();
	});

	$("#a_reply").click(function(){
		$("#div_reply").slideToggle();
		$("#reply_content").focus();
		$("#div_face").slideToggle();
	});
	$("#reply_reset").click(function(){
		$("#reply_content").focus();
	}); 


});
</script>

<script>
	function a(id) {
	return document.getElementById(id);
}
	function insertsmiley(icon) { 
		a("reply_content").value+= ':'+icon+':';
	}
	
	function check_reply(form) {
		if(form.reply_content.value=="")
		{
			alert('评论内容不能为空');
			form.reply_content.focus();
			return(false);
		}
		else {
			return(true);
		}
	}

</script>

</head>
<body>
<?php include_once("sidebar.php"); ?>
<p id = 'bt'><a href = "index.php">小树林BBS站</a><a id="a_banmian" href = "page.php?board=<?php echo $post_board; ?>"><?php echo $post_board; ?></a></p>
<hr>

<table name = "post_main" class = "post_main" id = "post_main" >
	<tr>
		<td>本篇点击量:<?php echo $t_click_count; ?>&nbsp&nbsp&nbsp&nbsp&nbsp本篇回复量:&nbsp<?php echo $reply_num_in_topic; ?></td>
	</tr>
	<tr>
		<td>发帖人:<a href = "profile.php?uname=<?php echo $post_author; ?>&board=<?php echo $post_board; ?>"><?php echo $post_author; ?></a>
		&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp版面:<?php echo $post_board; ?></td>
		<td id = "level">#0</td>
	</tr>

	<tr>
		<td>标&nbsp&nbsp题:<?php echo $post_name; ?></td>
	</tr>
	<tr>
		<td>发帖时间:<?php echo $post_time_week."&nbsp";echo $post_time;echo "&nbsp".$post_time_year; ?></td>
	</tr>
	
	<tr>
		<td id="neirong"><?php echo preg_replace("/[:](.+?)[:]/ies", "displaysmiley('\\1')", $post_content); ?></td>
	</tr>
	<tr>
		<td>--</td>
	</tr>
	<tr>
		<td>※&nbsp小树林BBS站，版权所有&nbsp请尊重原创&nbsp※</td>
		<?php
		if(isset($_SESSION["current_user"]))
		{
			if($_SESSION["current_user"] == $s_admin_name )
			{
				$sql = "select t_state from bbs_topic where t_id = '".$tid."'";
				$result = mysql_query($sql);
				$q_result = mysql_fetch_array($result);
				$is_zhiding = $q_result["t_state"];
				if($is_zhiding == 0 || $is_zhiding == null )
				{

		?>			
				<td><a href = "zhiding.php?tid=<?php echo $tid; ?>&back_board=<?php echo $post_board; ?>">置顶</a></td>
		<?php
				}
				else {
		?>
					<td><a href = "quxiao_zhiding.php?tid=<?php echo $tid; ?>&back_board=<?php echo $post_board; ?>">取消置顶</a></td>
		<?php
				}
		?>
				<td><a href = "delete.php?tid=<?php echo $tid; ?>&back_board=<?php echo $post_board; ?>">删贴</a></td>
		<?php
					
			}
		}
		?>
	</tr>
</table>

<?php
	$sql = "select * from bbs_reply where r_tid = '".$tid."' order by r_time limit ".$offset.",".$page_size."";
	$result = mysql_query($sql);        //此条变量为“回复集”功能，在后面用到，此$result变量 不能被覆盖！！！！请用别的变量名！！

	while($q_result = mysql_fetch_array($result))
	{
		$comment_user = $q_result["r_u_name"];
		$comment_rid = $q_result["r_id"];

		$comment_time2 = $q_result["r_time"];
		$comment_time = substr($comment_time2,5,14);		//只截月份、日，时间，年份另外
		$comment_year = substr($comment_time2,0,4);			//年份
		$comment_content = $q_result["r_content"];
		$comment_session = $q_result["r_s_name"];
		$comment_week = $q_result["r_week"];		//星期几 
?>

<table class = "reply_table">
	<tr class>
		<td>回帖人:<a href = "profile.php?uname=<?php echo $comment_user; ?>&board=<?php echo $comment_session; ?>"><?php echo $comment_user; ?></a>
		&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp版面:<?php echo $comment_session; ?></td>
		<td>#<?php
				$level = $level+1; 
				echo $level + $offset;
			 ?>
		</td>
	</tr>

	<tr>
		<td>标&nbsp&nbsp题:<?php echo $post_name; ?></td>
	</tr>
	<tr>
		<td>发帖时间:<?php echo $comment_week."&nbsp"; echo $comment_time;echo "&nbsp".$comment_year; ?></td>
	</tr>
	
	<tr>
		<td class = "rp_neirong"><?php echo preg_replace("/[:](.+?)[:]/ies", "displaysmiley('\\1')", $comment_content); ?></td>
	</tr>
	<tr>
		<td>--</td>
	</tr>
	<tr>
		<td>※&nbsp小树林BBS站，版权所有&nbsp请尊重原创&nbsp※</td>
		<?php
		if(isset($_SESSION["current_user"]))
		{
			if($_SESSION["current_user"] == $s_admin_name )
			{
		?>
			<td><a href = "delete.php?rid=<?php echo $comment_rid; ?>&back_board=<?php echo $comment_session; ?>">删贴</a></td>	
		<?php

			}
		}
		?>
	</tr>
</table>


<?php
	}
?>

<p id ="page_line">本主题共有&nbsp<?php echo $reply_num_in_topic; ?>&nbsp篇回帖&nbsp&nbsp本页显示的是其中的<?php echo $offset+1; ?>&nbsp-
<?php 
	if($page_num<$page_count)
	{
		echo $offset+$page_size; 
	}
	else {
		echo $reply_num_in_topic;
	}
?>篇&nbsp&nbsp

[页数]
<?php
	for($page_haoma=1;$page_haoma<=$page_count;$page_haoma++)
	{
		if($page_haoma != $page_num) {
?>	
		<a = href = "browse.php?board=<?php echo $board_name; ?>&tid=<?php echo $tid; ?>&page=<?php echo $page_haoma; ?>">[<?php echo $page_haoma; ?>]</a>
<?php
		}
		else {
?>			[<?php echo $page_num; ?>]
<?php
		}
	}
		
?>
</p>

<p id = "fenye">
	<?php 
		$first = 1 ;
		$prev = $page_num -1 ;
		$next = $page_num +1 ;
		$last = $page_count;

		if($page_num>1&&$page_num<$page_count)
		{
	?>		<a href = "browse.php?board=<?php echo $board_name; ?>&tid=<?php echo $tid; ?>&page=<?php echo $first; ?>">首页</a>&nbsp
			<a href = "browse.php?board=<?php echo $board_name; ?>&tid=<?php echo $tid; ?>&page=<?php echo $prev; ?>">上一页</a>&nbsp
			<a href = "browse.php?board=<?php echo $board_name; ?>&tid=<?php echo $tid; ?>&page=<?php echo $next; ?>">下一页</a>&nbsp
			<a href = "browse.php?board=<?php echo $board_name; ?>&tid=<?php echo $tid; ?>&page=<?php echo $last; ?>">尾页</a>&nbsp
	<?php
		}
		if($page_num == 1 && $page_count>1)
		{
	?>
			<a href = "browse.php?board=<?php echo $board_name; ?>&tid=<?php echo $tid; ?>&page=<?php echo $first; ?>">首页</a>&nbsp
			<a href = "browse.php?board=<?php echo $board_name; ?>&tid=<?php echo $tid; ?>&page=<?php echo $next; ?>">下一页</a>&nbsp
			<a href = "browse.php?board=<?php echo $board_name; ?>&tid=<?php echo $tid; ?>&page=<?php echo $last; ?>">尾页</a>

	<?php
		}
		if($page_num == $page_count && $page_count>1)
		{
	?>		<a href = "browse.php?board=<?php echo $board_name; ?>&tid=<?php echo $tid; ?>&page=<?php echo $first; ?>">首页</a>
			<a href = "browse.php?board=<?php echo $board_name; ?>&tid=<?php echo $tid; ?>&page=<?php echo $prev; ?>">上一页</a>
			<a href = "browse.php?board=<?php echo $board_name; ?>&tid=<?php echo $tid; ?>&page=<?php echo $last; ?>">尾页</a>
	<?php
		}
	?>
</p>

<table name = "tb_reply" id = "tb_reply" class = "tb_reply" >
	<tr>
		<td id = "td_reply"><a href = "#" id = "a_reply" name = "a_reply" >发表回帖</a></td>
		<td><a href = "page.php?board=<?php echo $post_board; ?>"> >>返回本版</a></td>
	</tr>
</table>

<div id="div_face">
	  	<?php 
			$smiles=getsmiles();
			foreach($smiles as $key => $value){
				echo "<img src=\"images/smiles/".$value."\" style=\"cursor:pointer\" onclick=\"insertsmiley('".$key."')\"/> ";
			}
			unset($smiles);
		?>
</div>



<div class = "div_reply" id = "div_reply" name = "div_reply">
	<form action = "reply_process.php" method = "post" id = "reply_fm" name = "reply_fm" onsubmit="return check_reply(this)" >
		<tr>
			<td><textarea name = "reply_content" id = "reply_content" rows = "6" cols = "75"></textarea></td>
			<input type = "hidden" value = "<?php echo $tid; ?>" id = "r_tid" name = "r_tid">
			<input type = "hidden" value = "<?php echo $post_board; ?>" id = "r_board_name" name = "r_board_name">
			<input type = "hidden" value = "<?php echo $post_name; ?>" name = "r_t_name" id = "r_t_name" >
		</tr>

		<tr>
			<td><input type = "submit" value = "回帖" name = "submit_reply" onclick="check_reply(reply_fm)" ></td>
			<td><input type = "reset" value = "删掉重写" id = "reply_reset"></td>
			<td><input type = "button" value = "点我收起！" id = "div_hidden" name = "div_hidden" ></td>
		</tr>
	</form>
</div>
<hr>
<p id = "version">版权所有 -- 小树林BBS站--http://bbs.fjau.edu.cn &nbsp All Right Reserved &nbsp&nbsp made by J.H.Wang</p>
</body>
</html>